package com.pj.compose.ui.theme

import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.ViewCompat


enum class ThemeType {
    Default,
    Theme1,
    Theme2,
    Theme3,
    Theme4,
    Theme5,
}

data class WrappedColor(val lightColors: ColorScheme, val darkColors: ColorScheme)

object CustomThemeManager {

    object Default {
        val darkColors = darkColorScheme(
            primary = Default200,
            secondary = DefaultSecondary,
            background = Color.Black,
            onPrimary = DefaultOnPrimary,
            surface = Color.Black,
            outlineVariant = DividerColor,
        )

        val lightColors = lightColorScheme(
            primary = Default500,
            secondary = DefaultSecondary,
            primaryContainer = Default700,
            background = Color.White,
            onPrimary = DefaultOnPrimary,
            surface = DefaultOnPrimary,
            outlineVariant = DividerColor,

            )
    }

    object Theme1 {
        val darkColors = lightColorScheme(
            primary = Theme1_200,
            secondary = Theme1Secondary,
            primaryContainer = Default700,
            background = Color.Black,
            onPrimary = Theme1OnPrimary,
            surface = Color.Black,
            outlineVariant = DividerColor,
        )

        val lightColors = lightColorScheme(
            primary = Theme1_500,
            primaryContainer = Theme1_700,
            secondary = Theme1Secondary,
            background = Color.White,
            onPrimary = Theme1OnPrimary,
            surface = Theme1OnPrimary,
            outlineVariant = DividerColor,
        )
    }

    object Theme2 {
        val darkColors = darkColorScheme(
            primary = Theme2_200,
            primaryContainer = Theme2_700,
            secondary = Theme2Secondary,
            background = Color.Black,
            onPrimary = Theme2OnPrimary,
            surface = Color.Black,
            outlineVariant = DividerColor,
        )

        val lightColors = lightColorScheme(
            primary = Theme2_500,
            primaryContainer = Theme2_700,
            secondary = Theme2Secondary,
            background = Color.White,
            onPrimary = Theme2OnPrimary,
            surface = Theme2OnPrimary,
            outlineVariant = DividerColor,
        )
    }

    object Theme3 {
        val darkColors = darkColorScheme(
            primary = Theme3_200,
            primaryContainer = Theme3_700,
            secondary = Theme3Secondary,
            background = Color.Black,
            onPrimary = Theme3OnPrimary,
            surface = Color.Black,
            outlineVariant = DividerColor,
        )

        val lightColors = lightColorScheme(
            primary = Theme3_500,
            primaryContainer = Theme3_700,
            secondary = Theme3Secondary,
            background = Color.White,
            onPrimary = Theme3OnPrimary,
            surface = Theme3OnPrimary,
            outlineVariant = DividerColor,
        )
    }

    object Theme4 {
        val darkColors = darkColorScheme(
            primary = Theme4_200,
            primaryContainer = Theme4_700,
            secondary = Theme4Secondary,
            background = Color.Black,
            onPrimary = Theme4OnPrimary,
            surface = Color.Black,
            outlineVariant = DividerColor,
        )

        val lightColors = lightColorScheme(
            primary = Theme4_500,
            primaryContainer = Theme4_700,
            secondary = Theme4Secondary,
            background = Color.White,
            onPrimary = Theme4OnPrimary,
            surface = Theme4OnPrimary,
            outlineVariant = DividerColor,
        )
    }


    object Theme5 {
        val darkColors = darkColorScheme(
            primary = Theme5_200,
            primaryContainer = Theme5_700,
            secondary = Theme5Secondary,
            background = Color.Black,
            onPrimary = Theme5OnPrimary,
            surface = Color.Black,
            outlineVariant = DividerColor,
        )

        val lightColors = lightColorScheme(
            primary = Theme5_500,
            primaryContainer = Theme5_700,
            secondary = Theme5Secondary,
            background = Color.White,
            onPrimary = Theme5OnPrimary,
            surface = Theme5OnPrimary,
            outlineVariant = DividerColor,
        )
    }


    @Composable
    fun WithTheme(
        type: ThemeType,
        darkTheme: Boolean = isSystemInDarkTheme(),
        content: @Composable () -> Unit
    ) {
        val wrappedColor = getWrappedColor(type)
        val colors = if (darkTheme) wrappedColor.darkColors else wrappedColor.lightColors
        MaterialTheme(
            colorScheme = colors,
            typography = Typography,
//            shapes = Shapes,
            content = content
        )
    }

    fun getWrappedColor(type: ThemeType): WrappedColor {
        val darkColors: ColorScheme
        val lightColors: ColorScheme
        when (type) {
            ThemeType.Default -> {
                darkColors = Default.darkColors
                lightColors = Default.lightColors
            }
            ThemeType.Theme1 -> {
                darkColors = Theme1.darkColors
                lightColors = Theme1.lightColors
            }
            ThemeType.Theme2 -> {
                darkColors = Theme2.darkColors
                lightColors = Theme2.lightColors
            }
            ThemeType.Theme3 -> {
                darkColors = Theme3.darkColors
                lightColors = Theme3.lightColors
            }
            ThemeType.Theme4 -> {
                darkColors = Theme4.darkColors
                lightColors = Theme4.lightColors
            }
            ThemeType.Theme5 -> {
                darkColors = Theme5.darkColors
                lightColors = Theme5.lightColors
            }
        }
        return WrappedColor(lightColors, darkColors)
    }
}

private val DarkColorScheme = darkColorScheme(
    primary = Red_500,
    secondary = Red_200,
    tertiary = Red_500,
    background = cEAEDEE,
    onPrimary = White,
    surface = White,
    outlineVariant = DividerColor,
)

private val LightColorScheme = lightColorScheme(
    primary = c66ccff,
    secondary = c3367D5,
    tertiary = c2257AA,
    background = cEAEDEE,
    onPrimary = White,
    surface = White,
    outlineVariant = DividerColor,

    /* Other default colors to override
    background = Color(0xFFFFFBFE),
    surface = Color(0xFFFFFBFE),
    onPrimary = Color.White,
    onSecondary = Color.White,
    onTertiary = Color.White,
    onBackground = Color(0xFF1C1B1F),
    onSurface = Color(0xFF1C1B1F),
    */
)

@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    // Dynamic color is available on Android 12+
    dynamicColor: Boolean = false,
    content: @Composable () -> Unit
) {
    val colorScheme = when {
        dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
            val context = LocalContext.current
            if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
        }
        darkTheme -> DarkColorScheme
        else -> LightColorScheme
    }
    val view = LocalView.current
    if (!view.isInEditMode) {
        SideEffect {
            (view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
            ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
        }
    }
    MaterialTheme(
        colorScheme = colorScheme,
        typography = Typography,
        content = content
    )
}